/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.entity.SysEmployeeEntity;
import io.renren.modules.sys.service.SysEmployeeService;
import io.renren.modules.sys.service.SysEmployeeService;
import io.renren.modules.sys.service.SysOrgService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统人员
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/employee")
public class SysEmployeeController extends AbstractController {
	@Autowired
	private SysEmployeeService sysEmployeeService;
	@Autowired
	private SysOrgService sysOrgService;


	/**
	 * 所有人员列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:employee:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysEmployeeService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 人员信息
	 */
	@GetMapping("/info/{empId}")
	@RequiresPermissions("sys:employee:info")
	public R info(@PathVariable("empId") Long empId){
		SysEmployeeEntity employee = sysEmployeeService.getById(empId);
		Map<String, Object> params = new HashMap<>();
		PageUtils page = sysOrgService.queryPage(params);
		R r = R.ok();
		r.put("orgList", page.getList());
		return r.put("employee", employee);
	}
	
	/**
	 * 保存人员
	 */
	@SysLog("保存人员")
	@PostMapping("/save")
	@RequiresPermissions("sys:employee:save")
	public R save(@RequestBody SysEmployeeEntity employee){
		ValidatorUtils.validateEntity(employee, AddGroup.class);
		sysEmployeeService.saveEmp(employee);
		
		return R.ok();
	}
	
	/**
	 * 修改人员
	 */
	@SysLog("修改人员")
	@PostMapping("/update")
	@RequiresPermissions("sys:employee:update")
	public R update(@RequestBody SysEmployeeEntity employee){
		ValidatorUtils.validateEntity(employee, UpdateGroup.class);
		sysEmployeeService.update(employee);
		
		return R.ok();
	}
	
	/**
	 * 删除人员
	 */
	@SysLog("删除人员")
	@PostMapping("/delete")
	@RequiresPermissions("sys:employee:delete")
	public R delete(@RequestBody Long[] empIds){
		sysEmployeeService.deleteBatch(empIds);
		
		return R.ok();
	}
}
